package com.ruoyi.ssh.service.impl;

import java.util.List;

import com.ruoyi.ssh.domain.Course;
import com.ruoyi.ssh.mapper.CourseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ssh.mapper.SubjectMapper;
import com.ruoyi.ssh.domain.Subject;
import com.ruoyi.ssh.service.ISubjectService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 科目管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-12
 */
@Service
public class SubjectServiceImpl implements ISubjectService {
    @Autowired
    private SubjectMapper subjectMapper;
    @Autowired
    private CourseMapper courseMapper;

    /**
     * 查询科目管理
     *
     * @param id 科目管理主键
     * @return 科目管理
     */
    @Override
    public Subject selectSubjectById(Integer id) {
        return subjectMapper.selectSubjectById(id);
    }

    /**
     * 查询科目管理列表
     *
     * @param subject 科目管理
     * @return 科目管理
     */
    @Override
    public List<Subject> selectSubjectList(Subject subject) {
        return subjectMapper.selectSubjectList(subject);
    }

    /**
     * 新增科目管理
     *
     * @param subject 科目管理
     * @return 结果
     */
    @Override
    public int insertSubject(Subject subject) {
        return subjectMapper.insertSubject(subject);
    }

    /**
     * 修改科目管理
     *
     * @param subject 科目管理
     * @return 结果
     */
    @Override
    public int updateSubject(Subject subject) {
        return subjectMapper.updateSubject(subject);
    }

    /**
     * 批量删除科目管理
     *
     * @param ids 需要删除的科目管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSubjectByIds(Integer[] ids) {
        // 检查每个科目下是否存在未删除的课程
        for (Integer id : ids) {
            Course course = new Course();
            course.setSubjectId(id);
            List<Course> courses = courseMapper.selectCourseList(course);
            // 检查是否存在未删除的课程
            boolean hasUndeletedCourse = false;
            for (Course course1 : courses) {
                if (course1.getDel() == 1) {
                    hasUndeletedCourse = true;
                    break;
                }
            }
            if (hasUndeletedCourse) {
                Subject subject = subjectMapper.selectSubjectById(id);
                if (subject != null) {
                    throw new RuntimeException("科目\"" + subject.getName() + "\"下存在课程，无法删除");
                } else {
                    throw new RuntimeException("科目下存在课程，无法删除");
                }
            }
        }
        return subjectMapper.deleteSubjectByIds(ids);
    }
    /**
     * 删除科目管理信息
     *
     * @param id 科目管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSubjectById(Integer id) {
        Course course = new Course();
        course.setSubjectId(id);
        List<Course> courseCount = courseMapper.selectCourseList(course);
        // 判断是否有未删除的课程
        boolean hasUndeletedCourse = false;
        for (Course course1 : courseCount) {
            if (course1.getDel() == 1) {
                hasUndeletedCourse = true;
                break;
            }
        }
        if (hasUndeletedCourse) {
            throw new RuntimeException("科目下存在课程，无法删除");
        }
        // 无关联时执行删除
        return subjectMapper.deleteSubjectById(id);
    }

    @Override
    public int enableSubjectByIds(Integer[] ids) {
        return subjectMapper.enableSubjectByIds(ids);
    }

    @Override
    public List<Subject> selectAllSubjectNameAndId() {
        return subjectMapper.selectAllSubjectNameAndId();
    }

    @Override
    public List<Subject> selectAllSubjectNameAndId1() {
        return subjectMapper.selectAllSubjectNameAndId();
    }
}
